package com.wangjiyuan.giftshuo.ui.activity.classify.shop_details;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.widget.TextView;

import com.wangjiyuan.giftshuo.R;
import com.wangjiyuan.giftshuo.bean.classifybean.shop.ShopDetLayoutBean;
import com.wangjiyuan.giftshuo.bean.classifybean.shop.TreeBean;
import com.wangjiyuan.giftshuo.config.classify.ClassifyConstant;
import com.wangjiyuan.giftshuo.ui.activity.classify.shop_details.recycler_adapter.ShopDetLayoutRecyclerAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 单品中点击图片的详情画面
 */

public class ShopDetLayoutActivity extends AppCompatActivity implements ShopDetLayoutContract.IShopDetLayoutView{
    private Context mContext=this;
    private String title;
    private int id,limit=20,offset=0;
    private Map<String,Integer> paramsMap=new HashMap<>();

    private Toolbar toolbar_shopDetails;
    private TextView textView_shopDetailsTitle;
    private RecyclerView recyclerView_shopDetails;
    private List<ShopDetLayoutBean.DataBean.ItemsBean> itemsBeanList=new ArrayList<>();
    private ShopDetLayoutRecyclerAdapter shopDetLayoutRecyclerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_details);
        initData();
        initView();
    }

    private void initView() {
        toolbar_shopDetails= (Toolbar) findViewById(R.id.toolBar_shopDetails);
        textView_shopDetailsTitle= (TextView) findViewById(R.id.textView_shopDetails_title);
        initToolBar();

        recyclerView_shopDetails= (RecyclerView) findViewById(R.id.recyclerView_shopDetails);
        recyclerView_shopDetails.setHasFixedSize(true);
        GridLayoutManager gridLayoutManager=new GridLayoutManager(mContext,2, OrientationHelper.VERTICAL,false);
        recyclerView_shopDetails.setLayoutManager(gridLayoutManager);
        shopDetLayoutRecyclerAdapter=new ShopDetLayoutRecyclerAdapter(mContext,itemsBeanList);
        recyclerView_shopDetails.setAdapter(shopDetLayoutRecyclerAdapter);
    }

    private void initToolBar() {
        toolbar_shopDetails.setTitle("");
        textView_shopDetailsTitle.setText(title);
        setSupportActionBar(toolbar_shopDetails);
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initData() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        TreeBean.DataBean.CategoriesBean.SubcategoriesBean subcategoriesBean=
                (TreeBean.DataBean.CategoriesBean.SubcategoriesBean) bundle.getSerializable(ClassifyConstant.SHOP_DET_LAYOUT_BEAN);
        title = subcategoriesBean.getName();

        id=subcategoriesBean.getId();
        paramsMap.put("limit",limit);
        paramsMap.put("offset",offset);
        ShopDetLayoutPresenter shopDetLayoutPresenter=new ShopDetLayoutPresenter(this);
        shopDetLayoutPresenter.getShopDetLayoutObservable(id,paramsMap);
    }

    @Override
    public void loadShopDetLayoutData(ShopDetLayoutBean shopDetLayoutBean) {
        List<ShopDetLayoutBean.DataBean.ItemsBean> items = shopDetLayoutBean.getData().getItems();
        shopDetLayoutRecyclerAdapter.reloadRecyclerView(items,true);
    }
}
